	var id = GetQueryString("id");
	/*var ztree;
	var setting = {
		    data: {
		        simpleData: {
		            enable: true,
		            idKey: "id",
		            pIdKey: "parentId",
		            rootPId: -1
		        },
		        key: {
		            url:"nourl"
		        }
		    }
		};*/
	
	var vm = new Vue({
    	  el: '#app',
    	  data: {
    		  roleList:{},
    		  user:{
    				status:1,
    			}
    	  },
    	  methods: {
    		  // 获取指定管理员
    		  getUser: function(id){
    	        	var url = baseURL + "sys/user/info/" + id;
    	    		$.get(url, function(data){
    	    			vm.user = data.user;
    	    			vm.user.password = null;
    	    			$('#password').attr('placeholder','为空则不修改');
    	    			//vm.getDeptTree();
    	    		});
    	      },
    	      // 获取角色信息
    	      getRoleList: function(){
    				$.get(baseURL + "sys/role/select", function(r){
    					vm.roleList = r.list;
    				});
    		  },
    		  //加载部门树
    	      /*getDeptTree: function(){
    	            
    	            $.get(baseURL + "sys/dept/list", function(r){
    	                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
    	                var node = ztree.getNodeByParam("id", vm.user.deptId);
    	                if(node != null){
    	                    ztree.selectNode(node);
    	                    vm.user.deptName = node.name;
    					}
    	            })
    	       },
		       // 弹出部门树选择框
		       deptTree: function(){
		            layer.open({
		                type: 1,
		                offset: '50px',
		                title: "选择部门",
		                area: ['300px', '400px'],
		                shade: 0,
		                shadeClose: false,
		                content: jQuery("#deptLayer"),
		                btn: ['确定', '取消'],
		                btn1: function (index) {
		                    var node = ztree.getSelectedNodes();
		                    //选择上级部门
		                    vm.user.deptId = node[0].id;
		                    vm.user.deptName = node[0].name;
	
		                    layer.close(index);
		                },
		                btn2: function(index){
		                	ztree.checkAllNodes(false);
		                	layer.close(index);
		                }
		            });
		      },*/
    	      saveOrUpdate: function () {
    	    	  var url = vm.user.id == null ? "sys/user/save" : "sys/user/update";
    				if(vm.user.roleIdList==null){
    					alert("请分配相应的角色!");
    					return;
    				}
    				$.loadAjax({
    					type: "POST",
    				    url: baseURL + url,
    	                contentType: "application/json",
    				    data: JSON.stringify(vm.user),
    				    success: function(r){
    				    	if(r.code === 0){
    				    		layer.alert('提交成功', function(index){
    				    			backToTable();
    				    			layer.close(index);
    							});
    						}else{
    							layer.alert(r.msg);
    						}
    					}
    				});
    	      }
    	  },
    	  mounted:function(){
              	
    		this.getRoleList();
    		// id 存在则为编辑
		  	if (id != null && id.length > 0) {
		  		// 显示加载中提示
		  		this.getUser(id);
		  	} else {
		  		this.roleList = {};
		  		this.user = {status:1, roleIdList:[]};
		  		//this.getDeptTree();
		  	}
		  	
    	  }
    });
	
	//表单验证
	$(function() {

        $("#editForm").validate({
            rules: {
            	userName: "required",
            	email: {
            		required:true,
            		email:true
            	},
            	deptName: "required",
            	password:{
            		password: true
                },
                roleId: "required",
                status: "required"
                
            },
            messages: {
            	userName: "请输入用户名",
            	email: "请输入正确邮箱",
            	deptName: "请选择部门",
            	roleId: "请选择角色",
            	status: "请选状态"
            },
            submitHandler : function(form) {  //验证通过后的执行方法
                //当前的form通过ajax方式提交
                vm.saveOrUpdate();
            },
            /* 重写错误显示消息方法,以tips方式弹出错误消息 */   
            showErrors: function(errorMap, errorList) {   
            	var msg = "";   
                $.each( errorList, function(i,v){   
                  msg = (v.message+"\r\n");   
                  if(msg!="") layer.tips(msg, "#"+v.element.id, { tips:2, tipsMore: true });   
                });   
            },   
            /* 提示失去焦点时不验证 */    
            onfocusout: false 
        });
    });
	
	/**** 自定义验证规则 ****/
	
	// 检验菜单url
	jQuery.validator.addMethod("password", function(value, element) {
        
		var flag = true;
		if (id == null || id.length == 0){
			if (value == null || value.length == 0){
				flag = false;
			}
		}
        return flag;
    }, "密码不能为空");
    
        
        